<?php
/**
 * Created by PhpStorm.
 * User: medivh
 * Date: 15-7-6
 * Time: 上午11:00
 */
namespace DaChongQing\Controller;
use Think\Controller;
class DrawController extends Controller{
    public function GetCode(){
        getCode(U('Draw/index','',true,true),C('APPID'));
    }

    public function index(){
        if(I('code') == ''){
            $this->redirect('Draw/GetCode');
        }
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".C('APPID')."&secret=".C('APPSECRET')."&code=".I('code')."&grant_type=authorization_code";
        $arr = json_decode(file_get_contents($url));
        $openid = $arr->openid;
        cookie('openid',$openid);
        $nickname = M('user_info')->where(array('openid'=>$openid))->find();
        if(empty($nickname)){
            echo "<script>alert('对不起。这是只有关注我们的用户才有的福利哦')</script>";
        }
        cookie('nickname',$nickname['nickname']);
        $openid = cookie('openid');
        $data = array(
            'type' => array(
                'neq','999'
            )
        );
        $this->BonusList = M('user_bonus')->where($data)->limit('30')->order('time desc')->select();
        $gold = M('user_gold')->where(array('openid'=>$openid))->find();
        $this->gold = $gold['gold'];
        $bonus = M('draw_list')->order('id ASC')->select();
        $this->list1 = $bonus['0'];
        $this->list2 = $bonus['1'];
        $this->list3 = $bonus['2'];
        $this->list4 = $bonus['3'];
        $this->list5 = $bonus['4'];
        $this->list6 = $bonus['5'];
        $this->list7 = $bonus['6'];
        $this->list8 = $bonus['7'];
        $this->display();
    }

    public function GetD(){
        $openid = cookie('openid');
        $info = M('user_info')->where(array('openid'=>$openid))->find();
        $result = M('user_gold')->where(array('openid'=>$openid))->find();
        $zero = date('Y-m-d', time());
        $zero = strtotime($zero . " 00:00:00");
        $SignTime = array(
            'time' => array(
                'gt', $zero
            ),
            'openid' => $openid
        );
        $signt = array(
            'sign_time' => array(
                'gt', $zero
            ),
            'openid' => $openid
        );
        $TodayBonus = M('user_bonus')->where($SignTime)->select();
        $sign = M('user_sign')->where($signt)->find();
        if($result['gold'] < C('GOLD_DEC')){
            echo 'gold';
        }elseif(count($TodayBonus) >= C('LUCKY_DRAW')){
            echo 'changce';
        }elseif(empty($sign)){
            echo 'sign';
        }
//        elseif($info['location'] != "重庆市"){
//            echo 'location';
//        }
        else{
            $array = self::DrawD();
            $id = GetRand($array);
            $list = self::DrawList();
            $a = $list[$id]['gps'] - 1;
            $bonus = M('draw_list')->where(array('gps'=>$list[$id]['gps']))->find();
            $data = array(
                'openid' => $openid,
                'bonusname' => $bonus['title'],
                'nickname' => $info['nickname'],
                'time' => time(),
                'type' => $bonus['type']
            );
            M('user_gold')->where(array('openid'=>$openid))->setDec('gold',C('GOLD_DEC'));
            M('user_bonus')->data($data)->add();
            echo $a;
        }
//        $this->ajaxReturn($a,'EVAL');
    }

    public function DrawD(){
        $list = self::DrawList();
        $pro = '';
        foreach ($list as $key){
            $pro[] = $key['pro'];
        }
        return $pro;
    }

    public function DrawList(){
        $list = M('draw_list')->select();
        return $list;
    }

    public function DrawBonus(){
        if(IS_AJAX){
            $data = array(
                'nickname' => I('nickname'),
                'bonusname' => I('bonusname'),
                'time' => time()
            );
            M('user_bonus')->data($data)->add();
        }else{
            echo "本页面不能作为常规访问页面";
        }
    }

    public function SaveBonus(){
        $openid = cookie('openid');
        $bonus = I('bonus');
        $bonusname = I('bonusname');
        $nickname = cookie('nickname');
        $type = I('type');
        $result = M('user_gold')->where(array('openid'=>$openid))->find();
        $zero = date('Y-m-d', time());
        $zero = strtotime($zero . " 00:00:00");
        $SignTime = array(
            'time' => array(
                'gt', $zero
            ),
            'openid' => $openid
        );
        $TodayBonus = M('user_bonus')->where($SignTime)->select();
        if(count($TodayBonus) == C("LUCKY_DRAW")){
            switch($type){
                case 0:
                    M('user_gold')->where(array('openid'=>$openid))->setInc('gold',$bonus);
                    break;
                case 1:
                    $pay = new BonusController($openid,$bonus);
                    $md5sign = $pay->SetSign();
                    $parameters = array(
                        'nonce_str' => '123123131231321',
                        'sign' => $md5sign,
                        'mch_billno' => $pay->wxMchBillno('1252370801'),
                        'mch_id' => C('MCH_ID'),
                        'wxappid' => 'wx22c589512a2d5f61',
                        'nick_name' => '我爱大重庆',
                        'send_name' => '我爱大重庆',
                        're_openid' => $openid,
                        'total_amount' => $bonus,
                        'min_value' => $bonus,
                        'max_value' => $bonus,
                        'total_num' => '1',
                        'wishing' => '恭喜发财！',
                        'client_ip' => '121.40.30.107',
                        'act_name' => '恭喜发财',
                        'remark' => '恭喜发财'
                    );
                    $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
                    $pay->wxHttpsRequestPem($url,$pay->wxArrayToXml($parameters));
                    break;

            }
            echo 1;
        }
    }

    public function aa(){
        $array = self::DrawD();
        $id = GetRand($array);
        dump($id);
        $list = self::DrawList();
        dump($list);die;
        $a = $list[$id]['id'];
        $result = M('draw_list')->where(array('id'=>$a))->find();
        echo "随机id为".$a."--------奖品为".$result['title']."----------概率数字".$result['pro'];
    }

    public function SendBonus($openid,$bonus){
        $pay = new BonusController($openid,$bonus);
        $md5sign = $pay->SetSign();
        $parameters = array(
            'nonce_str' => '123123131231321',
            'sign' => $md5sign,
            'mch_billno' => $pay->wxMchBillno('1252370801'),
            'mch_id' => C('MCH_ID'),
            'wxappid' => 'wx22c589512a2d5f61',
            'nick_name' => '我爱大重庆',
            'send_name' => '我爱大重庆',
            're_openid' => $openid,
            'total_amount' => $bonus,
            'min_value' => $bonus,
            'max_value' => $bonus,
            'total_num' => '1',
            'wishing' => '恭喜发财！',
            'client_ip' => '121.40.30.107',
            'act_name' => '恭喜发财',
            'remark' => '恭喜发财'
        );
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
        $a = $pay->wxHttpsRequestPem($url,$pay->wxArrayToXml($parameters));
        dump($a);
    }



    public function test(){
        $openid = "o9pT9t9S_wMcQAQIOtJjopc0584c";
        $info = M('user_info')->where(array('openid'=>$openid))->find();
        $result = M('user_gold')->where(array('openid'=>$openid))->find();
        $zero = date('Y-m-d', time());
        $zero = strtotime($zero . " 00:00:00");
        $SignTime = array(
            'time' => array(
                'gt', $zero
            ),
            'openid' => $openid
        );
        $signt = array(
            'sign_time' => array(
                'gt', $zero
            ),
            'openid' => $openid
        );
        $TodayBonus = M('user_bonus')->where($SignTime)->select();
        $sign = M('user_sign')->where($signt)->find();
        if($result['gold'] < C('GOLD_DEC')){
            echo 'gold';
        }
//elseif(count($TodayBonus) >= C('LUCKY_DRAW')){
//            echo 'changce';
//        }
        elseif(empty($sign)){
            echo 'sign';
        }elseif($info['location'] != "重庆市"){
            echo 'location';
        }else{
            $array = self::DrawD();
            $id = GetRand($array);
            $list = self::DrawList();
            $a = $list[$id]['gps'];
            $bonus = M('draw_list')->where(array('id'=>$id))->find();
//            $data = array(
//                'openid' => $openid,
//                'bonusname' => $bonus['title'],
//                'nickname' => $info['nickname'],
//                'time' => time(),
//                'type' => $bonus['type']
//            );
//            M('user_gold')->where(array('openid'=>$openid))->setDec('gold',C('GOLD_DEC'));
//            M('user_bonus')->data($data)->add();
            $bonus = M('draw_list')->where(array('gps'=>$a))->find();
            dump($bonus);
        }
    }

}